<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="container">
  <section>
    <span translate>mqtt-client-session.subscriptions</span>
    <span> ({{ subscriptionsFormArray()?.length }})</span>
  </section>
  @if (subscriptionsFormArray().controls?.length) {
    <section class="flex flex-col gap-2.5">
      <div class="flex flex-row gap-2.5">
        <mat-label class="flex flex-1 basis-[60%]">{{ 'subscription.topic-filter' | translate }}&#42;</mat-label>
        <mat-label class="flex flex-1 basis-[30%]" translate>mqtt-client-session.qos</mat-label>
        <mat-label style="width: 40px"></mat-label>
        <mat-label style="width: 40px"></mat-label>
      </div>
      @for (topicFilter of subscriptionsFormArray().controls; track topicFilter) {
        <div [formGroup]="topicFilter" class="subscriptions-container flex flex-row items-center gap-2.5">
          <mat-form-field class="flex flex-1 basis-[60%]" appearance="outline" subscriptSizing="dynamic">
            <input matInput name="topicFilter" formControlName="topicFilter" required
                   [matAutocomplete]="auto" (focusout)="clearFilteredOptions()">
            <mat-autocomplete #auto="matAutocomplete">
                @for (option of filteredTopics; track option) {
                  <mat-option [value]="option">{{option}}</mat-option>
                }
            </mat-autocomplete>
            <tb-copy-button [style]="{color: 'rgba(0,0,0,0.38)'}" [class.!hidden]="!topicFilter.get('topicFilter')?.value?.length" matSuffix
                            [copyText]="topicFilter.get('topicFilter').value">
            </tb-copy-button>
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'mqtt-client-session.topic-filter-required' | translate }}"
                      [class.!hidden]="!(topicFilter.invalid && topicFilter.touched)"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
          <tb-qos-select class="flex flex-1 basis-[30%]" formControlName="qos" displayLabel="false" appearance="outline" subscriptSizing="dynamic">
          </tb-qos-select>
          <section style="width: 40px">
            <tb-subscription-options
              [subscriptionOptions]="topicFilter"
              (subscriptionOptionsValue)="subscriptionOptionsChanged($event, topicFilter)">
            </tb-subscription-options>
          </section>
          <section style="width: 40px">
            <button mat-icon-button
                    [class.!hidden]="disabled()"
                    (click)="removeTopic($index)"
                    matTooltip="{{ 'mqtt-client-session.remove-subscription' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
          </section>
        </div>
      }
    </section>
  }
  <section>
    <div>
      <div class="tb-hint" innerHTML="{{ 'mqtt-client-session.support-level-wildcards' | translate }}"></div>
      <div class="tb-hint" innerHTML="{{ 'mqtt-client-session.single-level-wildcards-hint' | translate }}"></div>
      <div class="tb-hint" innerHTML="{{ 'mqtt-client-session.multi-level-wildcards-hint' | translate }}"></div>
    </div>
    <button mat-stroked-button color="primary"
      class="add-button"
      [class.!hidden]="disabled()"
      (click)="addTopic()"
      type="button">
      {{ 'mqtt-client-session.add-subscription' | translate }}
    </button>
  </section>
</div>

